package yunhe.servlet;

import org.json.JSONArray;
import org.json.JSONObject;
import yunhe.biz.StockMgrBiz;
import yunhe.biz.impl.StockMgrBizImpl;
import yunhe.entity.EmpMsg;
import yunhe.entity.StockOut;
import yunhe.entity.StockOutAppend;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lzh on 2017/6/18.
 */
@WebServlet(name = "OutStockServlet",urlPatterns = "/stockmgr/OutStockServlet")
public class OutStockServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String saleId=request.getParameter("saleId");
        String empMsg=request.getParameter("empMsg");
        String stockReason=request.getParameter("stockReason");
        StockOut sk=new StockOut();
        sk.setStockReason(stockReason);
        EmpMsg e=new EmpMsg();
        e.setWorkId(empMsg);
        sk.setEmpMsg(e);
        sk.setStockOutDate(new Date(new java.util.Date().getTime()));
        sk.setStockOutsId(saleId);
        StockMgrBiz smb=new StockMgrBizImpl();

        String detail=request.getParameter("detail");
        try {
            JSONArray ja=new JSONArray(detail);
            List<StockOutAppend> lsoa=new ArrayList<>();
            for (int i = 0; i <ja.length() ; i++) {
                StockOutAppend soa=new StockOutAppend();
                JSONObject jo= (JSONObject) ja.get(i);
                soa.setStoId(jo.getString("InvOutOfSto"));
                soa.setStockOutQuantity(jo.getInt("saleQuantity"));
                soa.setStockOut(sk);
                soa.setProBarCode(jo.getString("proBarCode"));
                lsoa.add(soa);
            }
            int re=smb.stockOut(sk,lsoa);
            if(re>0){
                response.getWriter().print("true");
            }
            else{
                response.getWriter().print("false");
            }
            //System.out.println(((JSONObject)ja.get(0)).get("salesId"));
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
